package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.AddressAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserAddressBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetUserAddressListModel;

public class SelectUserAddressActivity extends BaseActivity implements View.OnClickListener {
    private String useraddressid,userid;
    RecyclerView selectaddress_rv;
    private List<UserAddressBean> items;
    private boolean up = false;
    Button selectaddress_add;
    @Override
    void initView() {
        setLayout_file(R.layout.activity_select_address);
        useraddressid = getIntent().getStringExtra("useraddressid");
        selectaddress_rv = (RecyclerView)findViewById(R.id.selectaddress_rv);
        selectaddress_add = (Button)findViewById(R.id.selectaddress_add);
        userid=getid();
        GetUserAddressListModel getUserAddressListModel = new GetUserAddressListModel();
        getUserAddressListModel.getUserAddressList(userid,userAddressBeanListListener);
    }

    @Override
    void initEvent() {
        selectaddress_add.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }

    ListListener<UserAddressBean> userAddressBeanListListener = new ListListener<UserAddressBean>() {
        @Override
        public void onResponse(List<UserAddressBean> list) {
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(SelectUserAddressActivity.this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            selectaddress_rv.setLayoutManager(linearLayoutManager);
            items=list;
            AddressAdapter addressAdapter=new AddressAdapter(SelectUserAddressActivity.this,items,R.layout.address_item,userid,useraddressid);
            selectaddress_rv.setAdapter(addressAdapter);
            selectaddress_rv.setItemAnimator(new DefaultItemAnimator());
        }

        @Override
        public void onFail(String msg) {

        }
    } ;

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.selectaddress_add:
                Intent intent = new Intent(SelectUserAddressActivity.this,AddAddressressActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        up = true;

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (up) {
            initView();
            initEvent();
            initDate();
            up = false;
        }
    }
}
